package com.linkhub.oms.admin.modules.stock.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
import com.linkhub.oms.admin.modules.stock.domain.param.StockFlowQueryParam;
import com.linkhub.oms.admin.modules.stock.domain.vo.StockFlowVO;
import com.linkhub.oms.admin.modules.stock.domain.vo.StockVO;
import com.linkhub.oms.admin.modules.stock.enums.ProductType;
import com.linkhub.oms.admin.modules.stock.service.StockFlowBizService;
import com.linkhub.oms.common.response.PageDTO;
import com.linkhub.oms.component.mp.util.QtMybatisPlusUtil;
import com.linkhub.oms.dao.mapper.PhysicalStockFlowEntityMapper;
import com.linkhub.oms.dao.mapper.PreemptionStockFlowEntityMapper;
import com.linkhub.oms.entity.*;
import com.linkhub.oms.entity.resulttype.stock.OutStockResultEntity;
import com.linkhub.oms.entity.resulttype.stock.StockFlowResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName StockFlowBizServiceImpl
 * @Description TODO
 * @Author XieYu
 * @Date 2024/6/24 22:41
 * @Version 1.0
 */
@Slf4j
@Service
public class StockFlowBizServiceImpl implements StockFlowBizService {

    @Resource
    private PhysicalStockFlowEntityMapper physicalStockFlowEntityMapper;
    @Resource
    private PreemptionStockFlowEntityMapper preemptionStockFlowEntityMapper;

    @Override
    public PageDTO<StockFlowVO> getPhysicalStockFlows(StockFlowQueryParam stockFlowQueryParam) {
        MPJLambdaWrapper<PhysicalStockFlowEntity> stockFlowWrapper = JoinWrappers.lambda(PhysicalStockFlowEntity.class);
        stockFlowWrapper.selectAs(PhysicalStockFlowEntity::getCreateTime, StockFlowResultEntity::getTime)
                .selectAs(PhysicalStockFlowEntity::getBusinessType, StockFlowResultEntity::getBusinessType)
                .selectAs(PhysicalStockFlowEntity::getBusinessNo, StockFlowResultEntity::getBusinessNo)
                .selectAs(PhysicalStockFlowEntity::getStashName, StockFlowResultEntity::getStashName)
                .selectAs(PhysicalStockFlowEntity::getSkuId, StockFlowResultEntity::getSkuId)
                .selectAs(LocalProductEntity::getSku, StockFlowResultEntity::getSku)
                .selectAs(UserEntity::getUserName, StockFlowResultEntity::getMerchantUserName)
                .selectAs(LocalProductEntity::getProductEnTitle, StockFlowResultEntity::getProductEnTitle)
                .selectAs(PhysicalStockFlowEntity::getBeforeAmount, StockFlowResultEntity::getBeforeAmount)
                .selectAs(PhysicalStockFlowEntity::getAmount, StockFlowResultEntity::getAmount)
                .selectAs(PhysicalStockFlowEntity::getAfterAmount, StockFlowResultEntity::getAfterAmount)
                .selectAs(PhysicalStockFlowEntity::getType, StockFlowResultEntity::getStatus)
                .innerJoin(UserEntity.class, on -> on.eq(PhysicalStockFlowEntity::getUserId, UserEntity::getUserId))
                .innerJoin(LocalProductEntity.class, on -> on.eq(PhysicalStockFlowEntity::getSkuId, LocalProductEntity::getSkuId))
                .like(StringUtils.isNotBlank(stockFlowQueryParam.getSku()), LocalProductEntity::getSku, stockFlowQueryParam.getSku())
                .eq(StringUtils.isNotBlank(stockFlowQueryParam.getBusinessType()), PhysicalStockFlowEntity::getBusinessType, stockFlowQueryParam.getBusinessType())
                .eq(StringUtils.isNotBlank(stockFlowQueryParam.getBusinessNo()), PhysicalStockFlowEntity::getBusinessNo, stockFlowQueryParam.getBusinessNo())
                .eq(StringUtils.isNotBlank(stockFlowQueryParam.getStashName()), BatchEntity::getStashName, stockFlowQueryParam.getStashName())
                .ge(StringUtils.isNotBlank(stockFlowQueryParam.getStartDate()), PhysicalStockFlowEntity::getCreateTime, stockFlowQueryParam.getStartDate())
                .le(StringUtils.isNotBlank(stockFlowQueryParam.getEndDate()), PhysicalStockFlowEntity::getCreateTime, stockFlowQueryParam.getEndDate())
                .eq(StringUtils.isNotBlank(stockFlowQueryParam.getStatus()), PhysicalStockFlowEntity::getType, stockFlowQueryParam.getStatus())
                .eq(StringUtils.isNotBlank(stockFlowQueryParam.getMerchantUserIds()), PhysicalStockFlowEntity::getUserId, stockFlowQueryParam.getUserId())
                .orderByDesc(PhysicalStockFlowEntity::getCreateTime);

        Page<StockFlowResultEntity> objectPage = new Page<>(stockFlowQueryParam.getPage(), stockFlowQueryParam.getSize());
        objectPage.setOptimizeJoinOfCountSql(false);

        Page<StockFlowResultEntity> page = physicalStockFlowEntityMapper.selectJoinPage(objectPage,
                StockFlowResultEntity.class, stockFlowWrapper);

        return QtMybatisPlusUtil.convertPage(page, StockFlowVO::convert);
    }

    @Override
    public PageDTO<StockFlowVO> getPreemptionStockFlows(StockFlowQueryParam stockFlowQueryParam) {
        MPJLambdaWrapper<PreemptionStockFlowEntity> stockFlowWrapper = JoinWrappers.lambda(PreemptionStockFlowEntity.class);
        stockFlowWrapper.selectAs(PreemptionStockFlowEntity::getCreateTime, StockFlowResultEntity::getTime)
                .selectAs(PreemptionStockFlowEntity::getBusinessType, StockFlowResultEntity::getBusinessType)
                .selectAs(PreemptionStockFlowEntity::getBusinessNo, StockFlowResultEntity::getBusinessNo)
                .selectAs(PreemptionStockFlowEntity::getStashName, StockFlowResultEntity::getStashName)
                .selectAs(PreemptionStockFlowEntity::getSkuId, StockFlowResultEntity::getSkuId)
                .selectAs(LocalProductEntity::getSku, StockFlowResultEntity::getSku)
                .selectAs(LocalProductEntity::getProductEnTitle, StockFlowResultEntity::getProductEnTitle)
                .selectAs(PreemptionStockFlowEntity::getBeforeAmount, StockFlowResultEntity::getBeforeAmount)
                .selectAs(PreemptionStockFlowEntity::getAmount, StockFlowResultEntity::getAmount)
                .selectAs(PreemptionStockFlowEntity::getAfterAmount, StockFlowResultEntity::getAfterAmount)
                .selectAs(UserEntity::getUserName, StockFlowResultEntity::getMerchantUserName)
                .innerJoin(UserEntity.class, on -> on.eq(PreemptionStockFlowEntity::getUserId, UserEntity::getUserId))
                .innerJoin(LocalProductEntity.class, on -> on.eq(PreemptionStockFlowEntity::getSkuId, LocalProductEntity::getSkuId))
                .like(StringUtils.isNotBlank(stockFlowQueryParam.getSku()), LocalProductEntity::getSku, stockFlowQueryParam.getSku())
                .eq(StringUtils.isNotBlank(stockFlowQueryParam.getBusinessType()), PreemptionStockFlowEntity::getBusinessType, stockFlowQueryParam.getBusinessType())
                .eq(StringUtils.isNotBlank(stockFlowQueryParam.getBusinessNo()), PreemptionStockFlowEntity::getBusinessNo, stockFlowQueryParam.getBusinessNo())
                .eq(StringUtils.isNotBlank(stockFlowQueryParam.getStashName()), BatchEntity::getStashName, stockFlowQueryParam.getStashName())
                .ge(StringUtils.isNotBlank(stockFlowQueryParam.getStartDate()), PreemptionStockFlowEntity::getCreateTime, stockFlowQueryParam.getStartDate())
                .le(StringUtils.isNotBlank(stockFlowQueryParam.getEndDate()), PreemptionStockFlowEntity::getCreateTime, stockFlowQueryParam.getEndDate())
                .orderByDesc(PreemptionStockFlowEntity::getCreateTime);

        Page<StockFlowResultEntity> objectPage = new Page<>(stockFlowQueryParam.getPage(), stockFlowQueryParam.getSize());
        objectPage.setOptimizeJoinOfCountSql(false);

        Page<StockFlowResultEntity> page = preemptionStockFlowEntityMapper.selectJoinPage(objectPage,
                StockFlowResultEntity.class, stockFlowWrapper);

        return QtMybatisPlusUtil.convertPage(page, StockFlowVO::convert);
    }

}
